import json

import pytest
import requests
import logging
def du_dl():
    with open("user.json",encoding="utf8") as f:
        data=json.load(f)
        return data
class Test():
    token=""
    shang=""
    def setu_method(self):
        print("初始化环境，开始执行用例")
    def teardown_method(self):
        print("优化环境，用例执行结束")
    #登录
    @pytest.mark.parametrize("data",du_dl())
    def test_denglu(self,data):
        try:
            url="http://10.59.9.12:7001/api/public/login"
            body={"username":data.get("username"),"password":data.get("password")}
            headers={"content-type":"application/json"}
            r=requests.post(url=url,json=body,headers=headers)
            print(r.json())
            assert r.status_code == 200
            assert '普通会员' == r.text
            assert r.json()["data"]["group_name"] == "普通会员"
        except Exception as e:
            logging.info(e)
        Test.token="Bearer " +r.json()["data"]["token"]
    #商品详情
    def test_shangpin(self):
        try:
            url="http://10.59.9.12:7001/api/goods/view?id=4"
            headers={"connection":"keep-alive"}
            r=requests.get(url=url,headers=headers)
            print(r.json())
            assert r.status_code == 200
            assert '阿诗顿厨师机A6家用7升商用小型和面机静音自动多功能鲜奶揉面机' == r.text
            assert r.json()["info"] == "ok"
        except Exception as e:
            logging.info(e)
    #加入购物车
    def test_jiaru(self):
        try:
            url="http://10.59.9.12:7001/api/cart/add"
            body={"id":4,"sku_id":"","num":1}
            headers={"auth-token":Test.token}
            r=requests.post(url=url,json=body,headers=headers)
            print(r.json())
            assert r.status_code == 200
            assert '商品添加至购物车成功' == r.text
            assert r.json()["info"] == "商品添加至购物车成功"
        except Exception as e:
            logging.info(e)
    #查看购物车
    def test_kan(self):
        try:
            url="http://10.59.9.12:7001/api/cart?page=1"
            headers={"auth-token":Test.token}
            r=requests.get(url=url,headers=headers)
            print(r.json())
            assert r.status_code == 200
            assert '阿诗顿厨师机A6家用7升商用小型和面机静音自动多功能鲜奶揉面机' == r.text
            assert r.json()["info"] == "阿诗顿厨师机A6家用7升商用小型和面机静音自动多功能鲜奶揉面机"
        except Exception as e:
            logging.info(e)
        Test.shang=r.json()["data"]["list"][0]["id"]
    #支付
    def test_zhifu(self):
        try:
            url="http://10.59.9.12:7001/api/order/create"
            body={"id":Test.shang,"type":"cart","address_id":335,
                  "store_id":2,"shipping_type":1,"bonus_id":"0","payment":"balance",
                  "remarks":"","source":1,"url":"http://10.59.9.12:7001/wap/pages/cart/confirm?id=2081&type=cart"}
            headers={"auth-token":Test.token}
            r=requests.post(url=url,json=body,headers=headers)
            print(r.json())
            assert r.status_code == 200
            assert '支付成功' == r.text
            assert r.json()["info"] == "ok"
        except Exception as e:
            logging.info(e)


